<?php
namespace app\index\controller;
use app\index\model\Article as ArticleModel;

use app\index\model\Page1;
use app\index\model\Page2;
use QL\QueryList;

class Index extends Base
{
    //把栏目左边的所有类别和该类别下的链接首页抓下
    public function test(){
        $hj = QueryList::Query('https://www.itjc8.com/forum-36-1.html',array(
            "title"     =>  array('.tbn #lf_1 dd a','text'),
            "url"       =>  array('.tbn #lf_1 dd a','href')
        ))
        ->data
        ;
        $page1 = new Page1();
        $page1->saveAll($hj);
//        print_r($hj);die;
        foreach ($hj as $v){
//            $insertData = [];
//            $insertData['title'] = $v;
//            Page1->insert($v);
        }
//        $data = $hj->getData(function($x){
//            return $x['url'];
//        });
//        print_r($data);
    }


    public function fix_test1(){
        $page1Res = Page1::select();
        foreach ($page1Res as $p){
            $this->test2($p['url'], $p['id']);
        }
    }

    public function fix_test1_two(){
        $this->test2("https://www.itjc8.com/forum-97-1.html", 15);
//        $page1Res = Page1::select();
//        foreach ($page1Res as $p){
//            $this->test2($p['url'], $p['id']);
//        }
    }

    //遍历该类别下的所有列表链接
    public function test2($url, $cid){
        $p = $this->test3($url);
        $pname = $this->fix_last($url);
        for ($i=1;$i<=$p;$i++){
            sleep(15);
            $url_name = $pname . "-" . $i . ".html";
//            echo $url_name;die;
            $hj = QueryList::Query($url_name, array(
                "title"     =>  array('tbody[id^="normalthread_"] .forumtit .xst','text'),
                "url"       =>  array('tbody[id^="normalthread_"] .forumtit .xst','href'),
            ))->data;
//        print_r($hj);die;
            $new_hj = [];
            foreach ($hj as $h){
                $copy_h = $h;
                $copy_h['cid'] = $cid;
                $new_hj[] = $copy_h;
            }

//            print_r($new_hj);die;
            $page2 = new Page2();
            $page2->saveAll($new_hj);
            ob_flush();
            flush();
        }
    }

    //进入该类别下的首页，先找到分页总数，再循环把列表里的所有链接抓取
    //获取该类别该页下的总页数
    public function test3($url){
//        $hj = QueryList::Query($url, array(
//            "title"     =>  array('.pg .last','text'),
//        ))->data;
//        $p = str_replace('... ', '', $hj[0]['title']);
//        return $p;

        $hj = QueryList::Query($url, array(
            "title"     =>  array('.pg a:not(.nxt)','text'),
        ))->data;
        $p = 1;
        if($hj){
            $p = str_replace('... ', '', $hj[count($hj) - 1]['title']);
        }
        return $p;
    }

    //处理每页末尾
    public function fix_last($url){
        $subject = $url;
        $pattern = "/-[0-9]{1,}.html$/";
        $replace = "";
        return preg_filter($pattern, $replace, $subject);
    }

    public function test4(){
        $hj = QueryList::Query('https://www.itjc8.com/forum-2-1.html', array(
            "title"     =>  array('.pg a:not(.nxt)','text'),
        ))->data;
        print_r($hj[count($hj) - 1]);
//        $p = str_replace('... ', '', $hj[0]['title']);
//        return $p;
    }

    public function fix_article(){
        $page2Res = Page2::all();
        foreach ($page2Res as $p){
            sleep(5);
            $articleModel = new ArticleModel();
            $isCrawl = $articleModel->where('title', $p['title'])->find();
            if($isCrawl){
                continue;
            }else{
                $articleModel2 = new ArticleModel();
                $insertData = [];
                $insertData['title'] = $p['title'];
                $insertData['cateid'] = $p['cid'];
                $insertData['content'] = $this->test5();
                $articleModel2->save($insertData);
            }
            ob_flush();
            flush();
        }
    }

    public function test5(){
        $hj = QueryList::Query('https://www.itjc8.com/thread-7570-1-1.html', array(
            "title"     =>  array('.viewbox.firstfloor.cl .t_f', 'html', '-script -font -.attach_nopermission.attach_tips -.locked -span'),
        ))->data;
        return $hj[0]['title'];
//        print_r($hj);
    }

    //首页
    public function index()
    {    	
        $this->view->engine->layout('indexlayout');
		
		$articleModel = new ArticleModel();
		//      轮播
		$banArt = $articleModel->order('id asc')->limit(2)->select();
		//		头部 右边
		//		最新资源
		$topArt1 = $articleModel->where(array('new'=>1))->order('id asc')->limit(0,1)->select();
		$topArt2 = $articleModel->where(array('new'=>1))->order('id asc')->limit(1,1)->select();
		$topArt3 = $articleModel->where(array('new'=>1))->order('id asc')->limit(2,1)->select();		
		$topArt4 = $articleModel->where(array('new'=>1))->order('id asc')->limit(3,6)->select();
		//		热门资源
		$hotArt1 = $articleModel->where(array('hot'=>1))->order('id asc')->limit(0,1)->select();
		$hotArt2 = $articleModel->where(array('hot'=>1))->order('id asc')->limit(1,1)->select();
		$hotArt3 = $articleModel->where(array('hot'=>1))->order('id asc')->limit(2,1)->select();		
		$hotArt4 = $articleModel->where(array('hot'=>1))->order('id asc')->limit(3,6)->select();
		//		最新回复
		$anArt1 = $articleModel->where(array('ans'=>1))->order('id asc')->limit(0,1)->select();
		$anArt2 = $articleModel->where(array('ans'=>1))->order('id asc')->limit(1,1)->select();
		$anArt3 = $articleModel->where(array('ans'=>1))->order('id asc')->limit(2,1)->select();		
		$anArt4 = $articleModel->where(array('ans'=>1))->order('id asc')->limit(3,6)->select();
		//		最新讨论
		$taoArt1 = $articleModel->where(array('tao'=>1))->order('id asc')->limit(0,1)->select();
		$taoArt2 = $articleModel->where(array('tao'=>1))->order('id asc')->limit(1,1)->select();
		$taoArt3 = $articleModel->where(array('tao'=>1))->order('id asc')->limit(2,1)->select();		
		$taoArt4 = $articleModel->where(array('tao'=>1))->order('id asc')->limit(3,6)->select();
		//		资源申请
		$appArt1 = $articleModel->where(array('app'=>1))->order('id asc')->limit(0,1)->select();
		$appArt2 = $articleModel->where(array('app'=>1))->order('id asc')->limit(1,1)->select();
		$appArt3 = $articleModel->where(array('app'=>1))->order('id asc')->limit(2,1)->select();		
		$appArt4 = $articleModel->where(array('app'=>1))->order('id asc')->limit(3,6)->select();
        //		最新公告
		$gaoArt1 = $articleModel->where(array('gao'=>1))->order('id asc')->limit(0,1)->select();
		$gaoArt2 = $articleModel->where(array('gao'=>1))->order('id asc')->limit(1,1)->select();
		$gaoArt3 = $articleModel->where(array('gao'=>1))->order('id asc')->limit(2,1)->select();		
		$gaoArt4 = $articleModel->where(array('gao'=>1))->order('id asc')->limit(3,6)->select();
        
        $cate1 = db('cate')->where(array('pid'=>1))->order('id asc')->limit(0,4)->select();
        $cate2 = db('cate')->where(array('pid'=>1))->order('id asc')->limit(4,4)->select();
        $cate3 = db('cate')->where(array('pid'=>1))->order('id asc')->limit(8,4)->select();
        $cate4 = db('cate')->where(array('pid'=>1))->order('id asc')->limit(12,4)->select();
        $cateyw = db('cate')->where(array('pid'=>2))->order('id asc')->limit(0,4)->select();
        $catesj = db('cate')->where(array('pid'=>3))->order('id asc')->limit(0,4)->select();
        $catetl = db('cate')->where(array('pid'=>4))->order('id asc')->limit(0,4)->select();
        $catezx = db('cate')->where(array('pid'=>5))->order('id asc')->limit(0,4)->select();
        $this->assign(array(       
        	'banArt'=>$banArt,
        	'topArt1'=>$topArt1,
        	'topArt2'=>$topArt2,
        	'topArt3'=>$topArt3,
        	'topArt4'=>$topArt4,
        	
        	'hotArt1'=>$hotArt1,
        	'hotArt2'=>$hotArt2,
        	'hotArt3'=>$hotArt3,
        	'hotArt4'=>$hotArt4,
        	
        	'anArt1'=>$anArt1,
        	'anArt2'=>$anArt2,
        	'anArt3'=>$anArt3,
        	'anArt4'=>$anArt4,
        	
        	'taoArt1'=>$taoArt1,
        	'taoArt2'=>$taoArt2,
        	'taoArt3'=>$taoArt3,
        	'taoArt4'=>$taoArt4,
        	
        	'appArt1'=>$appArt1,
        	'appArt2'=>$appArt2,
        	'appArt3'=>$appArt3,
        	'appArt4'=>$appArt4,
        	
        	'gaoArt1'=>$gaoArt1,
        	'gaoArt2'=>$gaoArt2,
        	'gaoArt3'=>$gaoArt3,
        	'gaoArt4'=>$gaoArt4,
        	
            'cate1'=>$cate1,
            'cate2'=>$cate2,
            'cate3'=>$cate3,	
            'cate4'=>$cate4,	
            'cateyw'=>$cateyw,
            'catesj'=>$catesj,
            'catetl'=>$catetl,	
            'catezx'=>$catezx,			
        ));
        return view();
    }
    
    //技术博文
    public function jsbw()
    {   	
        $this->view->engine->layout('indexlayout');
        $articleModel = new ArticleModel();
        $articles = $articleModel->order('time desc')->paginate(10);

        
        $catebc = db('cate')->where(array('pid'=>1))->order('id asc')->select();
    	$cateyw = db('cate')->where(array('pid'=>2))->order('id asc')->select();
        $catesj = db('cate')->where(array('pid'=>3))->order('id asc')->select();
        $catetl = db('cate')->where(array('pid'=>4))->order('id asc')->select();
        $catezx = db('cate')->where(array('pid'=>5))->order('id asc')->select();
        $this->assign(array(
            
            'articles' => $articles,
            	
            'catebc'=>$catebc,	
            'cateyw'=>$cateyw,
            'catesj'=>$catesj,
            'catetl'=>$catetl,	
            'catezx'=>$catezx,			
        ));
        return view();
    }
    
    //开发手册
    public function kfsc()
    {
        $this->view->engine->layout('indexlayout');
        $articleModel = new ArticleModel();
        $articles = $articleModel->order('time desc')->paginate(10);

        
        $catebc = db('cate')->where(array('pid'=>1))->order('id asc')->select();
    	$cateyw = db('cate')->where(array('pid'=>2))->order('id asc')->select();
        $catesj = db('cate')->where(array('pid'=>3))->order('id asc')->select();
        $catetl = db('cate')->where(array('pid'=>4))->order('id asc')->select();
        $catezx = db('cate')->where(array('pid'=>5))->order('id asc')->select();
        $this->assign(array(
            
            'articles' => $articles,
            	
            'catebc'=>$catebc,	
            'cateyw'=>$cateyw,
            'catesj'=>$catesj,
            'catetl'=>$catetl,	
            'catezx'=>$catezx,			
        ));
        return view();
    }
    
     //注册
    public function register()
    {
        $this->view->engine->layout('indexlayout');
        return view();
    }
    
     //登录
    public function login()
    {
        $this->view->engine->layout('indexlayout');
        return view();
    }


}
